package com.dongjiantong.farmrecords.model;
/**
 *  养殖场采购表
 */
import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;

import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Table(name = "farmrecords_purchase")
@Data
public class FarmRecordsPurchase extends BaseEntity {
    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();
    private static final long serialVersionUID = -906479716488377905L;

    /**
     * 购置类型 1.原料2.药物 3.疫苗 4其他
     */
    private Integer type;
    /**
     * 单据编号
     */
    private String number;
    /**
     * 养殖场id
     */
    private Long farm_id;
    /**
     * 养殖场名称
     */
    private String farm_name;
    /**
     * 省id
     */
    private Integer province_id;

    /**
     * 市id
     */
    private Integer city_id;

    /**
     * 县id
     */
    private Integer region_id;

    /**
     * 镇id
     */
    private Integer street_id;

    /**
     * 村id
     */
    private Integer village_id;
    /**
     * 购买人
     */
    private String operator;
    /**
     * 购入日期
     */
    private Date date;
    /**
     * 有效日期
     */
    private Date effective_date;
    /**
     * 养殖品种
     */
    private Integer breed_second_type;
    /**
     *  采购名称
     */
    private Integer product_name;
    /**
     * 采购产品照片
     */
    @JsonIgnore
    private String product_json;
    @Transient
    private List<String> product;
    /**
     * 其他名称
     */
    private String other_name;
    /**
     *  规格
     */
    private String standard;
    /**
     * 批准文号／进口许可证/许可证
     */
    private String approval_number;

    @JsonIgnore
    private String approval_json;
    @Transient
    private List<String> approval;
    /**
     * 数量
     */
    private Integer quantity;
    /**
     * 单价
     */
    private Float price;
    /**
     * 总价
     */
    private Float total_price;
    /**
     * 经销商经营许可证编号
     */
    private String dealer_number;
    /**
     * 经销商经营许可证编号(照片)
     */
    @JsonIgnore
    private String dealer_json;
    @Transient
    private List<String> dealer;
    /**
     * 生产厂家生产许可证
     */
    private String manufacturer_number;
    /**
     * 生产厂家／GMP证书号
     */
    @JsonIgnore
    private String manufacturer_json;
    @Transient
    private List<String> manufacturer;
    /**
     * 批号
     */
    private String batch_number;
    /**
     * 批号 (照片)
     */
    @JsonIgnore
    private String batch_json;
    @Transient
    private List<String> batch;
    /**
     * 状态 1.暂存  2提交    （修改过后）
     */
    private Integer status;
    /**
     * 备注
     */
    private String remark;
    /**
     * 单据是否有效
     */
    private Integer delete_flag;
    /**
     * 创建时间
     */
    private Date created_at;
    /**
     * 更新时间
     */
    private Date updated_at;
    /**
     * 关联的文件表图片list
     */

    public void setProduct_json(String product_json) {
        this.product_json = product_json;

        if (Strings.isNullOrEmpty(product_json)) {
            this.product = null;
        } else {
            try {
                this.product = objectMapper.readValue(product_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setProduct(List<String> product) {
        this.product = product;

        if (product == null || product.isEmpty()) {
            this.product_json = "[]";
        } else {
            try {
                this.product_json = objectMapper.writeValueAsString(product);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setApproval_json(String approval_json) {
        this.approval_json = approval_json;

        if (Strings.isNullOrEmpty(approval_json)) {
            this.approval = null;
        } else {
            try {
                this.approval = objectMapper.readValue(approval_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setApproval(List<String> approval) {
        this.approval = approval;

        if (approval == null || approval.isEmpty()) {
            this.approval_json = "[]";
        } else {
            try {
                this.approval_json = objectMapper.writeValueAsString(approval);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setDealer_json(String dealer_json) {
        this.dealer_json = dealer_json;

        if (Strings.isNullOrEmpty(dealer_json)) {
            this.dealer = null;
        } else {
            try {
                this.dealer = objectMapper.readValue(dealer_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setDealer(List<String> dealer) {
        this.dealer = dealer;

        if (dealer == null || dealer.isEmpty()) {
            this.dealer_json = "[]";
        } else {
            try {
                this.dealer_json = objectMapper.writeValueAsString(dealer);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setManufacturer_json(String manufacturer_json) {
        this.manufacturer_json = manufacturer_json;

        if (Strings.isNullOrEmpty(manufacturer_json)) {
            this.manufacturer = null;
        } else {
            try {
                this.manufacturer = objectMapper.readValue(manufacturer_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setManufacturer(List<String> manufacturer) {
        this.manufacturer = manufacturer;

        if (manufacturer == null || manufacturer.isEmpty()) {
            this.manufacturer_json = "[]";
        } else {
            try {
                this.manufacturer_json = objectMapper.writeValueAsString(manufacturer);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

    public void setBatch_json(String batch_json) {
        this.batch_json = batch_json;

        if (Strings.isNullOrEmpty(batch_json)) {
            this.batch = null;
        } else {
            try {
                this.batch = objectMapper.readValue(batch_json,new TypeReference<List<String>>(){});
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setBatch(List<String> batch) {
        this.batch = batch;

        if (batch == null || batch.isEmpty()) {
            this.batch_json = "[]";
        } else {
            try {
                this.batch_json = objectMapper.writeValueAsString(batch);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }
}